<?php

/**
 * Classe permettant de modéliser une capture de la camera
 * 
 * Formatage des fichiers d'entrée
 * capture -> c_timestamp_quality.jpg
 * diff -> d_timestamp_quality.jpg
 * (miniature -> m(d/c)_timestamp_quality.jpg)
 * 
 * @author Haehnel Jonathan
 */
class CameraShot 
{
    private $_type;
    private $_fullname;
    private $_quality;
    private $_date;

    public function load($filename)
    {
        $path = CONF_SHOTS."/".$filename;
        if(file_exists($path) && is_file($path) && is_readable($path))
        {
           $datas = explode("_", str_replace(".jpg", "", $filename));
           if(count($datas) === 3)
           {
               $this->_fullname = $path;
               $this->_class = "";
               switch ($datas[0][0])
               {
                    case "c" :
                        $this->_type = "Capture";
                        break;
                    case "d" :
                        $this->_class = " diff";
                        $this->_type = "Capture différencielle";
                        break;
                    default :
                        $this->_type = "Inconnu";
                        break;
               }
               
               $this->_date = $datas[1];
               $this->_quality = $datas[2];
               
               return true;
           }
        }
        
        return false;
    }
    
    public function json()
    {
        return json_encode(
                array(
                    "type" => $this->_type,
                    "fullname" => $this->_fullname,
                    "date" => $this->_date,
                    "quality" => $this->_quality,
                    "class" => $this->_class,
                    "dateid" => date("dmy",$this->_date),
                    "label" => $this->toString()
                    )
                );
    }
    
    public function getQuality()
    {
        return $this->_quality;
    }
    
    public function getClass()
    {
        return $this->_class;
    }
    
    public function getFullName()
    {
        return $this->_fullname;
    }
    
    public function getDate()
    {
        return $this->_date;
    }
    
    public function getType()
    {
        return $this->_type;
    }
    
    public function toString()
    {
        return $this->_type . " du " . date("d-m-Y \à h\h m\m s\s",$this->_date) . " - " . $this->_quality . " Mpx";
    }
    
    public static function getCaptures($shot_include = true, $diff_include = false, $order = true, $limit = NULL)
    {
        $list_imgs = array();
        $files = array();
        $ignored = array('.', '..', '.svn', '.htaccess');
        
        $dir = opendir(CONF_SHOTS);
        while($file = readdir($dir)) 
        { 
            if(!is_dir(CONF_SHOTS."/".$file) && is_readable(CONF_SHOTS."/".$file)) 
            { 
                if (in_array($file, $ignored)) continue;
                if (!preg_match("/^(d|c|mc|md)_[0-9][0-9]*_[0-9]\.jpg$/", $file)) continue;
                $files[$file] = filemtime(CONF_SHOTS."/". $file);
            }
        } 
        
        if($order)
        {
           arsort($files);
        }
        $files = array_keys($files);
        
        $counter = 0;
        foreach($files as $file)
        {
            
            if($limit != NULL && $counter > $limit)
                break;
            
            if(preg_match("/^d.*$/", $file))
            {
                if($diff_include)
                {
                    $img = new CameraShot();
                    $img->load($file);
                    
                    $list_imgs[] = $img;
                    $counter++;
                }
            }
            
            if(preg_match("/^c.*$/", $file))
            {
                if($shot_include)
                {
                    $img = new CameraShot();
                    $img->load($file);
                    
                    $list_imgs[] = $img;
                    $counter++;
                } 
            }
        }
        
        //debug:var_dump($list_imgs);
        closedir($dir);
        
        return $list_imgs;
     }
}

?>
